package com.base.thread.demo_17;

import java.util.Random;

/**
 * 测试 自选锁
 * @ClassName: Demo2
 * @Description: 
 * @Author xuyou
 * @DateTime 2020年11月29日 下午9:02:43
 */
public class Demo2 {
	
	public static void main(String[] args) {
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(new Random().nextInt(2000));
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				System.out.println(Thread.currentThread().getName() + " 线程执行完了...");
			}
		}).start();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				
				try {
					Thread.sleep(new Random().nextInt(2000));
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				System.out.println(Thread.currentThread().getName() + " 线程执行完了...");
			}
		}).start();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				
				try {
					Thread.sleep(new Random().nextInt(2000));
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				System.out.println(Thread.currentThread().getName() + " 线程执行完了...");
			}
		}).start();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(new Random().nextInt(2000));
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				System.out.println(Thread.currentThread().getName() + " 线程执行完了...");
			}
		}).start();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				
				try {
					Thread.sleep(new Random().nextInt(2000));
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				System.out.println(Thread.currentThread().getName() + " 线程执行完了");
			}
		}).start();
		
		while(Thread.activeCount() != 1) {
			// 自旋等待线程执行完
		}
		System.out.println("所有线程都执行完了!");
	}

}
